// jwt.js,token中间件
const expressJwt = require("express-jwt");
const jwt = require('jsonwebtoken');
const { secretKey } = require('./constant');
const jwtAuth = expressJwt.expressjwt({
    secret: secretKey,
    algorithms: ["HS256"]
}).unless({
    path: [
        "/login",
        "/imageCode"
    ]
});
const jwtCheck = (str) => {
    let token = str.split(" ")[1];
    let data = {
        check: false,
        msg: ''
    }
    jwt.verify(token, secretKey, function (err, decoded) {
        if (err) {
            if (err.message == "jwt expired") {
                data.msg = "token已过期"
            } else {
                data.msg = "token不符合"
            }
        } else {
            data.check = true;
            data.msg = decoded;
        }
    });
    return data;
}

module.exports = {
    jwtAuth: jwtAuth,
    jwtCheck: jwtCheck
};